<?php
include_once $_SERVER['DOCUMENT_ROOT']."functions/password/bdd.php";
include_once $_SERVER['DOCUMENT_ROOT']."functions/parser/parse.php";
include_once $_SERVER['DOCUMENT_ROOT']."functions/bdd/avion/donnees.php";
session_start();
?>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />	
</head>
<body>
    <?php
    try {
        $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
        $bdd = new PDO('mysql:host=localhost;dbname=origavion', 'origavion', get_bdd_password(), $pdo_options);
    } catch (Exception $e) {
        die($e->getmessage());
    }
    $prep_get_nb_photos = $bdd->prepare('SELECT * FROM nb_photos_tmp WHERE token=?');
    $prep_get_addr_photos = $bdd->prepare('SELECT * FROM photos_tmp WHERE token=? AND no=?');
    
    if (isset($_POST['nom']) && strlen($_POST['nom']) <= 255) {
		$_POST['nom'] = parse($_POST['nom']);
        if (isset($_POST['description'])) {
			$_POST['description'] = parse($_POST['description']);
            if (isset($_POST['reglages'])) {
				$_POST['reglages'] = parse($_POST['reglages']);
                if (isset($_POST['categorie'])) {
					$_POST['categorie'] = parse($_POST['categorie']);
                    if (isset($_POST['difficulte']) && $_POST['difficulte'] <= 5 && $_POST['difficulte'] >= 0) {
                        $_POST['difficulte'] = (int) $_POST['difficulte'];
                        if (isset($_POST['stabilite']) && $_POST['stabilite'] <= 5 && $_POST['stabilite'] >= 0) {
                            $_POST['stabilite'] = (int) $_POST['stabilite'];
                            if (isset($_POST['stab_roul']) && $_POST['stab_roul'] <= 5 && $_POST['stab_roul'] >= 0) {
                                $_POST['stab_roul'] = (int) $_POST['stab_roul'];
                                if (isset($_POST['stab_tang']) && $_POST['stab_tang'] <= 5 && $_POST['stab_tang'] >= 0) {
                                    if (isset($_POST['vitesse'])) {
										$_POST['vitesse'] = parse($_POST['vitesse']);
                                        if (isset($_POST['decrochage'])) {
											$_POST['decrochage'] = parse($_POST['decrochage']);
                                            if (isset($_COOKIE['nb_etapes']) && $_COOKIE['nb_etapes'] <= 30 && $_COOKIE['nb_etapes'] > 0) {
                                                $_COOKIE['nb_etapes'] = (int) $_COOKIE['nb_etapes'];

                                                for ($i = 1; $i <= $_COOKIE['nb_etapes']; $i++) {
                                                    if (!(isset($_POST["etape_$i"]))) {
                                                        exit("Le nombre d'étapes ne correspond pas au nombre indiqué dans le cookie");
                                                    }
                                                }

												$donnee['id'] = set_avion_donnees($_POST['nom'], $_POST['description'], $_POST['categorie'], $_POST['difficulte'], $_POST['stabilite'], $_POST['stab_roul'], $_POST['stab_tang'], $_POST['vitesse'], $_POST['decrochage'], $_POST['reglages']);
                                                
                                                if (isset($_POST['token'])){
                                                    $token = $_POST['token'];
                                                    $prep_get_nb_photos->execute(array($token));
                                                    $nb_photos = $prep_get_nb_photos->fetch();
                                                } else {
                                                    exit("Erreur de token<br/>");
                                                }
                                                
                                                if ($donnee) {
                                                    for ($i = 0 ; $i <= $_COOKIE['nb_etapes'] ; $i++) {
                                                        if ($i > 0) {
                                                            if (isset($_POST["etape_$i"])) {
																$_POST["etape_$i"] = parse($_POST["etape_$i"]);
                                                                $rep = $bdd->prepare('INSERT INTO etapes(id_parent, numero, contenu) VALUES(?, ?, ?)');
                                                                $rep->execute(array($donnee['id'], "$i", $_POST["etape_$i"]));
																$rep->closeCursor();
                                                            } else {
                                                                echo 'prob set';
                                                                $erreur = true;
                                                            }
                                                        }

                                                        for ($j = 1 ; $j < $nb_photos['nb'] ; $j++) {
                                                            if (isset($_POST["photo_".$i."_$j"])) {
                                                                $prep_get_addr_photos->execute(array($token, $j));
                                                                $photo = $prep_get_addr_photos->fetch();
																
                                                                if ($photo){
                                                                    $rep = $bdd->prepare('INSERT INTO images(id_parent, no_etape, adresse, no) VALUES(?, ?, ?, ?)');
                                                                    $rep->execute(array($donnee['id'], "$i", $photo['adresse'], $_POST["photo_".$i."_$j"]));
                                                                    $test = 1;
																	$rep->closeCursor();
                                                                }
                                                            } else {
                                                                $test = 0;
                                                            }
                                                        }
                                                        if ($test != 1){
                                                            echo "Aucune photo choisie pour l'étape $i<br/>";
                                                        }
                                                    }
                                                    if (!($erreur)){
                                                        echo "<strong style=\"color:green;font-size:huge\">Avion Envoyé</strong>" ;
                                                    }
                                                } else {
                                                    echo "Erreur interne";
                                                }
                                                //$rep->closeCursor();
                                            } else {
                                                echo "Le nombre maximum d'étapes est de 30";
                                            }
                                        } else {
                                            echo "Vitesse de décrochage non renseignée";
                                        }
                                    } else {
                                        echo "Vitesse d'évolution non renseignée";
                                    }
                                } else {
                                    echo "Stabilité au tangage non renseignée";
                                }
                            } else {
                                echo "Stabilité au roulis non renseignée";
                            }
                        } else {
                            echo "Stabilite non renseignée";
                        }
                    } else {
                        echo "Difficulte non renseignée";
                    }
                } else {
                    echo "Categorie non renseignée";
                }
            } else {
                echo "Réglages supplémentaires non renseignés";
            }
        } else {
            echo "Description non renseignée";
        }
    } else {
        echo "nom non renseigné";
    }
    ?>
</body>